package com.cheng.chengliving.commodity.service.impl;

import com.cheng.chengliving.commodity.dao.AttrAttrgroupRelationDao;
import com.cheng.chengliving.commodity.dao.AttrDao;
import com.cheng.chengliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.cheng.chengliving.commodity.entity.AttrEntity;
import com.cheng.chengliving.commodity.service.AttrService;
import com.cheng.chengliving.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheng.common.utils.PageUtils;
import com.cheng.common.utils.Query;

import com.cheng.chengliving.commodity.dao.AttrgroupDao;
import com.cheng.chengliving.commodity.entity.AttrgroupEntity;
import com.cheng.chengliving.commodity.service.AttrgroupService;

import javax.annotation.Resource;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 实现增加根据分类+查询条件+分页的AIP接口
     * @param params
     * @param categoryId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //先获取检索传入的关键字
        String key = (String) params.get("key");
        //根据实际情况,封装查询条件到QueryWrapper对象
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();

        //判断key是否携带的有查询条件
        //希望他是一组独立的检索条件
        if (StringUtils.isNotBlank(key)) {
            //wrapper.eq("id",key).or().like("name",key);
            wrapper.and((obj)->{
                obj.eq("id",key).or().like("name",key);
            });
        }

        //下面我们再处理是否需要封装categoryId检索条件
        //这里先设置一个业务规则:categoryId == 0,表示在查询属性组的时候,不加入categoryId这个检索条件
        //否则就加入   And categoryId == xx 的检索条件,这里逻辑需要需要和前端代码配合
        if (categoryId!=0){
            wrapper.eq("category_id",categoryId);
        }
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据分类categoryId返回该分类关联的属性组和这些属性组关联的基本属性
     * @param categoryId
     * @return
     */
    @Resource
    private AttrService attrService;

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCategoryId(Long categoryId) {

        //1. 根据传入的category_id得到该分类关联的所有属性组
//        QueryWrapper<AttrgroupEntity> wrapper =
//                new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId);
//        List<AttrgroupEntity> attrgroupEntities = attrgroupDao.selectList(wrapper);

        List<AttrgroupEntity> attrgroupEntities = this.list(
                new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));

        //2. 根据前面查询到的attrgroupEntities进行遍历,
        // 将各个属性组关联的基本属性得到并封装到AttrGroupWithAttrsVo中返回
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrgroupEntities.stream().map((item) -> {
            //(1)先创建一个AttrGroupWithAttrsVo对象
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //(2)将attrgroupEntities 对象属性拷贝到vo对象中
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);

            //(3)通过属性组id获取到该属性组关联到的基本属性
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVo.getId());

            //(4)将前面得到的attrs设置到vo中attrs基本属性集合中
            attrGroupWithAttrsVo.setAttrs(attrs);

            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());


        return attrGroupWithAttrsVos;
    }

}
